﻿using System.Collections.Generic;

namespace Framework.Resource
{
    /// <summary>
    /// 资源信息
    /// </summary>
    public class AssetInfo
    {
        /// <summary>
        /// 资源的实例对象
        /// </summary>
        private List<object> m_InstanceObjectList = null;
        /// <summary>
        /// 引用计数
        /// </summary>
        private int m_ReferenceCount;
        /// <summary>
        /// 资源对象
        /// </summary>
        private object m_Asset;
        /// <summary>
        /// 资源对象
        /// </summary>
        public object Asset
        {
            get
            {
                return m_Asset;
            }
        }
        /// <summary>
        /// 实例数量是否为0
        /// </summary>
        public bool IsFree
        {
            get
            {
                return m_InstanceObjectList.Count <= 0 && m_ReferenceCount <= 0;
            }
        }

        /// <summary>
        /// 创建资源信息类
        /// </summary>
        /// <param name="asset">资源对象</param>
        public AssetInfo(object asset)
        {
            m_InstanceObjectList = new List<object>();
            m_ReferenceCount = 0;
            m_Asset = asset;
        }
        /// <summary>
        /// 获得资源的实例对象实例集合
        /// </summary>
        public List<object> InstanceObjectList
        {
            get
            {
                return m_InstanceObjectList;
            }
        }
        /// <summary>
        /// 移除资源实例
        /// </summary>
        /// <param name="obj">要移除的资源实例</param>
        public void RemoveAssetInstance(object obj)
        {
            if (m_InstanceObjectList.Contains(obj))
                m_InstanceObjectList.Remove(obj);
        }
        /// <summary>
        /// 添加资源实例
        /// </summary>
        /// <param name="obj">要添加的资源实例</param>
        public void AddAssetInstance(object obj)
        {
            if (!m_InstanceObjectList.Contains(obj))
                m_InstanceObjectList.Add(obj);
        }
        /// <summary>
        /// 增加引用计数
        /// </summary>
        public void AddAssetReference()
        {
            m_ReferenceCount++;
        }
        /// <summary>
        /// 减少引用计数
        /// </summary>
        public void MinusAssetReference()
        {
            m_ReferenceCount--;
            if (m_ReferenceCount < 0)
                m_ReferenceCount = 0;
        }
    }
}
